package com.example.consumer.interceptor;

import com.example.api.common.trace.TraceIdUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Activate(group = {CommonConstants.CONSUMER})
public class TraceIdInterceptor implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(TraceIdInterceptor.class);

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String traceId = TraceIdUtil.getTraceId();
        RpcContext.getServiceContext().setAttachment(TraceIdUtil.TRACE_ID, traceId);
        logger.info("Dubbo consumer traceId: {}", traceId);
        return invoker.invoke(invocation);
    }
} 